#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <thread>


class ThreadData
{
public:
    ThreadData()
    {

    }
    void Init(const std::string& name,int a,int b)
    {
        _name = name;
        _a = a;
        _b = b;
    }
    void Excute()
    {
        _result = _a + _b;
    }
    int Result()
    {
        return _result;
    }
    std::string Name()
    {
        return _name;
    }
    void SetId(pthread_t tid)
    {
        _tid = tid;
    }
    pthread_t GetId()
    {
        return _tid;
    }
    int A()
    {
        return _a;
    }
    int B()
    {
        return _b;
    }
    ~ThreadData()
    {}
private:
    std::string _name;
    int _a;
    int _b;
    int _result;
    pthread_t _tid;
};

std::string toHex(pthread_t tid)
{
    //4.进程内的函数,线程共享
    char buffer[64];
    snprintf(buffer,sizeof(buffer),"0x%lx",tid);
    return buffer;
}

void* routine(void* args)
{
    ThreadData* td = static_cast<ThreadData*>(args);
    while(true)
    {
        td->Excute();
        sleep(1);
        break;
    }
    return td;
}

#define NUM 10

int main()
{
    ThreadData td[NUM];
    //准备我们要加工处理的数据
    for(int i = 0;i < NUM;i++)
    {
        char id[64];
        snprintf(id,sizeof(id),"thread-%d",i);
        td[i].Init(id,i*10,i*20);
    }
    //创建多线程
    for(int i = 0;i < NUM;i++)
    {
        pthread_t id;
        pthread_create(&id,nullptr,routine,&td[i]);
        td[i].SetId(id);
    }

    //等待多线程
    for(int i=0;i<NUM;i++)
    {
        pthread_join(td[i].GetId(),nullptr);
    }
    for(int i = 0;i < NUM;i++)
    {
        printf("td[%d]: %d+%d=%d[%ld]\n",i,td[i].A(),td[i].B(),td[i].Result(),td[i].GetId());
    }
    return 0;
}